package notesDetection.SFT;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

// SFT CENTRAL IS THE NEWEST VERSION
public class SFTUtils {
	
	/**returns a set with random values from {0,1,...,limit-1} containing min(numELEM,limit)
	improvement: if we see in advance we need all, generate all (no random).*/
	//TODO improve performance by using complement if numElem > (limit/2)
	public static Set<Integer> RandomSet(int numElem, int limit)
	{
		
		Set<Integer> set = new HashSet<Integer>();
		if(numElem >= limit){
			for(int i=0; i<limit; i++){
				set.add(i);
			}
		}
		else{
			Random generator = new Random();
			while(set.size()<numElem && set.size()<limit){
				set.add(generator.nextInt(limit));
			}
		}
			return set;
	}
		
    /**
     * this function finds how good is the approximation an SFTFunction to another.
     * by the calculation of (2Norm(f-p)/2norm(f))^2. the closer to 0, the better the approximation!
     * assuming both agree on N...
     */
    public static double SFTFunctionVSApproximation(SFTFunction func1, SFTFunction func2, int sizeOfSample)
    {
    	
        Set<Integer> A = RandomSet(sizeOfSample,func1.getN());
        
    	double numerator=0;
    	double denominator=0;
    	Complex temp;
    	for(Integer a : A)
    	{
    		
    		temp = Complex.Sub(func1.funcValue(a), func2.funcValue(a));
    		numerator += temp.sqrdNorm();
    		denominator += func1.funcValue(a).sqrdNorm();
    	}
    	
    	double weight_left = (denominator!=0) ? (numerator/denominator) : -1; //if denom 0, return -1.
    	return weight_left;
    }
	
    //helpers
	public static double log2(int n)
	{
		return (Math.log(n)/Math.log(2));
	}
}
